package com.example.dansesshou.jcentertest;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.gwell.pano.PanoMode;
import com.gwelldemo.R;
import com.hdl.elog.ELog;
import com.p2p.core.MediaPlayer;
import com.p2p.core.P2PHandler;
import com.p2p.core.pano.BasePanoActivity;
import com.p2p.core.pano.PanoParentRelativelayout;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.IOException;

import Utils.Contants;
import Utils.ToastUtils;
import Utils.Util;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注意：
 * 1.需要实现：@see sdk.P2PListener#vRetPlaySize 以设置宽高
 * 2.需要引入库：compile 'com.jwkj:gwell-pano:1.4.0'
 * 3.此页面未展示功能同普通设备监控页功能
 */

public class PanoramaActivity extends BasePanoActivity {

    @BindView(R.id.btn_screenshot)
    Button btnScreenshot;
    @BindView(R.id.btn_change_four_split)
    Button btnChangeFourSplit;
    @BindView(R.id.btn_change_two_split)
    Button btnChangeTwoSplit;
    @BindView(R.id.btn_change_mix)
    Button btnChangeMix;
    @BindView(R.id.btn_change_column)
    Button btnChangeColumn;
    @BindView(R.id.btn_change_circle)
    Button btnChangeCircle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.layout_else)
    LinearLayout layoutElse;
    @BindView(R.id.activity_monitoer)
    LinearLayout activityMonitoer;
    PanoParentRelativelayout rlParent;
    RelativeLayout rlP2pview;
    private static final String TAG = "PanoramaActivity";
    public static String P2P_ACCEPT = "com.XXX.P2P_ACCEPT";
    public static String P2P_READY = "com.XXX.P2P_READY";
    public static String P2P_REJECT = "com.XXX.P2P_REJECT";
    private String callID, CallPwd;
    private String userId;

    private int channelId;

    /**
     * 当前剪裁比
     */
    private int curCutRatio = 50;

    /**
     * 当前画面中心X比值
     */
    private int curCutX = 500;
    /**
     * 当前画面中心Y比值
     */
    private int curCutY = 500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_panorama);
        ButterKnife.bind(this);
        userId = getIntent().getStringExtra(LoginActivity.USERID);
        callID = getIntent().getStringExtra("callID");
        CallPwd = getIntent().getStringExtra("CallPwd");
        LogUtils.i("ContactInfoActivity", "userId:" + userId);
        initPano();
        checkCamerPermission();
        regFilter();
//        setCutRatio(1);
    }

    private void regFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(P2P_REJECT);
        filter.addAction(P2P_ACCEPT);
        filter.addAction(P2P_READY);
        filter.addAction(Contants.P2P.RET_P2PDISPLAY);
        filter.addAction(Contants.P2P.RET_P2P_PARAM);
        registerReceiver(mReceiver, filter);
    }

    public BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(P2P_ACCEPT)) {
                int currentChannel = intent.getIntExtra("channelId", 0);
                if (channelId == -1 || channelId != currentChannel) return;
                Log.i(TAG, "------------P2P_ACCEPT-------------, channelId:" + channelId);
                P2PHandler.getInstance().openAudioAndStartPlaying(channelId,
                        1,
                        1);
                tvContent.append("P2P_ACCEPT\n");
            } else if (intent.getAction().equals(P2P_READY)) {
                int currentChannel = intent.getIntExtra("channelId", 0);
                if (channelId == -1 || channelId != currentChannel) return;
                Log.i(TAG, "------------P2P_READY-------------, channelId:" + channelId);
                tvContent.append("P2P_READY\n");

                startPlay(channelId);
                try {
                    MediaPlayer.getInstance()._setPanorama(channelId, true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (intent.getAction().equals(P2P_REJECT)) {
                int reason_code = intent.getIntExtra("reason_code", -1);
                int code1 = intent.getIntExtra("exCode1", -1);
                int code2 = intent.getIntExtra("exCode2", -1);
                String reject = String.format("\n 监控挂断(reson:%d,code1:%d,code2:%d)", reason_code, code1, code2);
                tvContent.append(reject);
            } else if (intent.getAction().equals(Contants.P2P.RET_P2PDISPLAY)) {
                Log.i(TAG, "------------RET_P2PDISPLAY-------------");
                int currentChannel = intent.getIntExtra("channelId", 0);
                if (channelId == -1 || channelId != currentChannel) return;
                tvContent.append("ret_display");
                setShowMode(PanoMode.PM_HALF_SPHERE);
            } else if (Contants.P2P.RET_P2P_PARAM.equals(intent.getAction())) {
                int cutRatio = intent.getIntExtra("cutRatio", 100);
                int cutX = intent.getIntExtra("cutX", 500);
                int cutY = intent.getIntExtra("cutY", 500);
                Log.i(TAG,
                        "cutRatio= " + cutRatio +
                                " ;cutX= " + cutX +
                                " ;cutY= " + cutY);
                if (cutRatio == 0) {//设备默认是0，如果为0时应该设置为默认值1
                    cutRatio = 100;
                }
                //兼容老设备，老设备是不会传画面中心点过来，默认是0
                if (0 == cutX && 0 == cutY) {
                    cutX = 500;
                    cutY = 500;
                }
                if (curCutRatio != cutRatio ||
                        curCutX != cutX ||
                        curCutY != cutY) {
                    curCutRatio = cutRatio;
                    curCutX = cutX;
                    curCutY = cutY;
                    setCutRatio(curCutX / 1000f, curCutY / 1000f, curCutRatio / 100f);
                    ELog.hdl("cutRatio = " + curCutRatio + "; curCutX:" + curCutX + "; curCutY:" + curCutY);
                }
            }
        }
    };

    private void checkCamerPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) { // 在android 6.0之前会默认返回true
                        // 已经获取权限
                        Log.e("PanoramaActivity", "已授予CAMERA权限");
                    } else {
                        // 未获取权限
                        ToastUtils.ShowError(PanoramaActivity.this, "您没有授权CAMERA权限，请在设置中打开授权", Toast.LENGTH_SHORT, true);
                    }
                });
    }

    @Override
    public PanoParentRelativelayout initParent() {
        rlParent = findViewById(R.id.rl_parent);
        return rlParent;
    }

    @Override
    public ViewGroup initRelativeLayout() {
        rlP2pview = findViewById(R.id.rl_p2pview);
        return rlP2pview;
    }

    @Override
    public int callDevice() {
        String pwd = P2PHandler.getInstance().EntryPassword(CallPwd);//经过转换后的设备密码
        channelId = P2PHandler.getInstance().call(userId, pwd, true, 1, callID, "", "", 2, callID, 0);
        Log.e(TAG, "callDevice: callID:" + callID + ",CallPwd:" + CallPwd + "; channelId:" + channelId + "\n");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvContent.append("callDevice callID：" + callID + ",CallPwd:" + CallPwd + "\n");
            }
        });

        // 监控连接成功以后，全景渲染View进入ready状态
        if (channelId >= 0) {
            panoReady();
        }
        return channelId;
    }

    @Override
    public int getSubType() {
        //此处传设备子类型
        return 35;
    }

    @Override
    public void captureHeader() {

    }

    @Override
    public void onParentClick(View group) {

    }

    @Override
    public int getChannelId() {
        return channelId;
    }

    @Override
    protected void onVideoPTS(long videoPTS) {

    }

    @Override
    protected void onAvBytesPerSec(int videoPTS) {

    }

    @Override
    public int getActivityInfo() {
        return 100000;
    }

    @Override
    protected void onGoBack() {

    }

    @Override
    protected void onGoFront() {

    }

    @Override
    protected void onExit() {

    }

    @OnClick({R.id.btn_screenshot, R.id.btn_change_four_split, R.id.btn_change_two_split, R.id.btn_change_mix, R.id.btn_change_column, R.id.btn_change_circle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_screenshot:
                checkSDPermision();
                break;
            case R.id.btn_change_four_split:
                //四分屏
                setShowMode(PanoMode.PM_FOUR_SCREEN);
                break;
            case R.id.btn_change_two_split:
                //二分屏
                setShowMode(PanoMode.PM_SCROLL);
                break;
            case R.id.btn_change_mix:
                //混合
                setShowMode(PanoMode.PM_MIX);
                break;
            case R.id.btn_change_column:
                //圆柱
                setShowMode(PanoMode.PM_CYLINDER);
                break;
            case R.id.btn_change_circle:
                //圆
                setShowMode(PanoMode.PM_HALF_SPHERE);
                break;
        }
    }

    private void checkSDPermision() {
        RxPermissions rxPermissions = new RxPermissions(this);
        boolean result = false;
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(granted -> {
                    if (granted) { // 在android 6.0之前会默认返回true
                        // 已经获取权限
                        capture(0);
                        Log.e("PanoramaActivity", "已授予STORAGE权限");
                    } else {
                        // 未获取权限
                        ToastUtils.ShowError(PanoramaActivity.this, "您没有授权STORAGE权限，请在设置中打开授权", Toast.LENGTH_SHORT, true);
                    }
                });
    }

    //Unity截图 0:普通截图  1:头像
    private void capture(int type) {
        String path = Util.getScreenShotPath() + "/test/123.jpg";
        Log.e(TAG, "capture: path:" + path);
        if (type == 0) {
            captureScreenPano(-1, path);
        } else {
            captureScreenPano(1, path);
        }
        saveImage(channelId, path);
    }

    //挂断
    private void reject() {
        unregisterReceiver(mReceiver);
        P2PHandler.getInstance().finish(channelId);
        finish();
    }

    @Override
    public void onHomePressed() {
        reject();
        super.onHomePressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            reject();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    public void onResume() {
        super.onResume();
    }

}
